package com.yinhanshi.s3test;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;

import androidx.appcompat.app.AppCompatActivity;

import com.yinhanshi.s3test.upload.UploadService;
import com.yinhanshi.s3test.utils.DownloadService;

public class MainActivity extends AppCompatActivity {

    private static final int FILE_SELECT_CODE = 0;

    Button select;
    Button interrupt;
    ProgressBar progress;
    TextView status;
    Button downloadSelect;
    Button downloadInterrupt;
    ProgressBar downloadProgress;
    Button btnGetFileInfo;
    TextView tvFileInfo;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        status = (TextView) findViewById(R.id.status);
        //上传
        select = (Button) findViewById(R.id.btn_select);
        interrupt = (Button) findViewById(R.id.btn_interrupt);
        progress = (ProgressBar) findViewById(R.id.progress);
        //下载
        downloadSelect = (Button) findViewById(R.id.btn_download);
        downloadInterrupt = (Button) findViewById(R.id.btn_download_interrupt);
        downloadProgress = (ProgressBar) findViewById(R.id.download_progress);

        select.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 选择要上传的文件
                String path = "/storage/emulated/0/hulu/Downloads/gaobai.mp4";
                // 初始化 upload
                Intent intent = new Intent(MainActivity.this, UploadService.class);
                intent.putExtra(UploadService.ARG_FILE_PATH, path);
                startService(intent);
            }
        });

        interrupt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 中断当前正在进行的上传
                Intent intent = new Intent(UploadService.UPLOAD_CANCELLED_ACTION);
                sendBroadcast(intent);
            }
        });

        downloadSelect.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 选择要下载的文件
                String fileS3Key = "2153f2548ceb6758a4f66894a9d0c265.mp4";
                // 初始化 upload
                Intent intent = new Intent(MainActivity.this, DownloadService.class);
                intent.putExtra(DownloadService.ARG_FILE_PATH, fileS3Key);
                startService(intent);
            }
        });

        downloadInterrupt.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                // 中断当前正在进行的下载任务
                Intent intent = new Intent(DownloadService.DOWNLOAD_CANCELLED_ACTION);
                sendBroadcast(intent);
            }
        });

    }

    @Override
    protected void onStart() {
        super.onStart();
        IntentFilter f = new IntentFilter();
        f.addAction(UploadService.UPLOAD_STATE_CHANGED_ACTION);
        f.addAction(DownloadService.DOWNLOAD_STATE_CHANGED_ACTION);
        registerReceiver(uploadStateReceiver, f);
    }

    @Override
    protected void onStop() {
        unregisterReceiver(uploadStateReceiver);
        super.onStop();
    }

    private BroadcastReceiver uploadStateReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            Bundle b = intent.getExtras();
            //上传UI状态刷新
            if (!TextUtils.isEmpty(b.getString(UploadService.MSG_EXTRA)))
                status.setText(b.getString(UploadService.MSG_EXTRA));
            int uploadPercent = b.getInt(UploadService.PERCENT_EXTRA);
            if (uploadPercent >= 0) {
                progress.setProgress(uploadPercent);
            }
            //下载UI状态刷下
            if (!TextUtils.isEmpty(b.getString(DownloadService.MSG_EXTRA))) {
                status.setText(b.getString(DownloadService.MSG_EXTRA));
            }
            int downloadPercent = b.getInt(DownloadService.PERCENT_EXTRA);
            if (downloadPercent >= 0) {
                downloadProgress.setProgress(downloadPercent);
            }

        }
    };
}
